using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Entity;
using APP.QUANLY.Entity.ConditionForm;

namespace APP.QUANLY.DAL
{
    /// <summary>
    /// Summary description for CHIETKHAULOAIMATHANG.
     /// </summary>
	public class CHIETKHAULOAIMATHANGDao
    {
	    private string _SQLSelectByKey;
	    private string _SQLUpdate;
	    private string _SQLInsert;
	    private string _SQLDelete;
	    private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion
        
        #region Constructor

        public CHIETKHAULOAIMATHANGDao()
        {
            _dbConnection = null;
		    InitSqlQuery();
        }

        public CHIETKHAULOAIMATHANGDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
       {
            string query;

            query = @"SELECT MACHIETKHAU, MALOAIMATHANG, MUCQUYDINHTU, MUCQUYDINHDEN, CHIETKHAU1, CHIETKHAU2, CHIETKHAU3, ISUSE FROM CHIETKHAULOAIMATHANG WHERE (MACHIETKHAU = @MACHIETKHAU)";
	        _SQLSelectByKey = query;

            query = @"UPDATE CHIETKHAULOAIMATHANG SET MALOAIMATHANG = @MALOAIMATHANG, MUCQUYDINHTU = @MUCQUYDINHTU, MUCQUYDINHDEN = @MUCQUYDINHDEN, CHIETKHAU1 = @CHIETKHAU1, CHIETKHAU2 = @CHIETKHAU2, CHIETKHAU3 = @CHIETKHAU3, ISUSE = @ISUSE WHERE (MACHIETKHAU = @MACHIETKHAU)";
	        _SQLUpdate = query;

            query = @"INSERT INTO CHIETKHAULOAIMATHANG VALUES (@MACHIETKHAU, @MALOAIMATHANG, @MUCQUYDINHTU, @MUCQUYDINHDEN, @CHIETKHAU1, @CHIETKHAU2, @CHIETKHAU3, @ISUSE)";
	        _SQLInsert = query;

            query = @"DELETE FROM CHIETKHAULOAIMATHANG WHERE (MACHIETKHAU = @MACHIETKHAU)";
	        _SQLDelete = query;

            query = @"SELECT MACHIETKHAU, MALOAIMATHANG, MUCQUYDINHTU, MUCQUYDINHDEN, CHIETKHAU1, CHIETKHAU2, CHIETKHAU3, ISUSE FROM CHIETKHAULOAIMATHANG";
	        _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

				if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(CHIETKHAULOAIMATHANG obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                         DalTools.addDbDataParameter(command, "MACHIETKHAU", obj.MACHIETKHAU, DbType.Int32);
                         DalTools.addDbDataParameter(command, "MALOAIMATHANG", obj.MALOAIMATHANG, DbType.Int32);
         DalTools.addDbDataParameter(command, "MUCQUYDINHTU", obj.MUCQUYDINHTU, DbType.Decimal);
         DalTools.addDbDataParameter(command, "MUCQUYDINHDEN", obj.MUCQUYDINHDEN, DbType.Decimal);
         DalTools.addDbDataParameter(command, "CHIETKHAU1", obj.CHIETKHAU1, DbType.Double);
         DalTools.addDbDataParameter(command, "CHIETKHAU2", obj.CHIETKHAU2, DbType.Double);
         DalTools.addDbDataParameter(command, "CHIETKHAU3", obj.CHIETKHAU3, DbType.Double);
         DalTools.addDbDataParameter(command, "ISUSE", obj.ISUSE, DbType.Boolean);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(CHIETKHAULOAIMATHANG obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                         DalTools.addDbDataParameter(command, "MACHIETKHAU", obj.MACHIETKHAU, DbType.Int32);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(CHIETKHAULOAIMATHANG obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(CHIETKHAULOAIMATHANG obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties
     
        private CHIETKHAULOAIMATHANGList SelectListByCommand(IDbCommand command)
        {
            CHIETKHAULOAIMATHANGList list = new CHIETKHAULOAIMATHANGList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				CHIETKHAULOAIMATHANG obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new CHIETKHAULOAIMATHANG();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
		
        private CHIETKHAULOAIMATHANGList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            CHIETKHAULOAIMATHANGList list = new CHIETKHAULOAIMATHANGList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				CHIETKHAULOAIMATHANG obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
				command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new CHIETKHAULOAIMATHANG();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	
        private void ReadProperties(CHIETKHAULOAIMATHANG obj, IDataReader reader)
        {
            try
            {
                                if(!(reader["MACHIETKHAU"] is DBNull))
		{
			obj.MACHIETKHAU = (Int32)reader["MACHIETKHAU"];
		}
                if(!(reader["MALOAIMATHANG"] is DBNull))
		{
			obj.MALOAIMATHANG = (Int32)reader["MALOAIMATHANG"];
		}
                if(!(reader["MUCQUYDINHTU"] is DBNull))
		{
			obj.MUCQUYDINHTU = (decimal)reader["MUCQUYDINHTU"];
		}
                if(!(reader["MUCQUYDINHDEN"] is DBNull))
		{
			obj.MUCQUYDINHDEN = (decimal)reader["MUCQUYDINHDEN"];
		}
                if(!(reader["CHIETKHAU1"] is DBNull))
		{
			obj.CHIETKHAU1 = (double)reader["CHIETKHAU1"];
		}
                if(!(reader["CHIETKHAU2"] is DBNull))
		{
			obj.CHIETKHAU2 = (double)reader["CHIETKHAU2"];
		}
                if(!(reader["CHIETKHAU3"] is DBNull))
		{
			obj.CHIETKHAU3 = (double)reader["CHIETKHAU3"];
		}
                if(!(reader["ISUSE"] is DBNull))
		{
			obj.ISUSE = (bool)reader["ISUSE"];
		}
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion
      
        public CHIETKHAULOAIMATHANG SelectByKey(Int32 MACHIETKHAU)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MACHIETKHAU", MACHIETKHAU, DbType.Int32);
      	    CHIETKHAULOAIMATHANGList list =  SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public CHIETKHAULOAIMATHANG SelectByKey(Int32 MACHIETKHAU, IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MACHIETKHAU", MACHIETKHAU, DbType.Int32);
      	    CHIETKHAULOAIMATHANGList list =  SelectListByCommand(command, trans);
			
            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }
		
        public CHIETKHAULOAIMATHANGList SelectAll()
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectAll;
      	    CHIETKHAULOAIMATHANGList list =  SelectListByCommand(command);
	        return list;
        }

         public CHIETKHAULOAIMATHANGList SelectByCommand(string Command)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = Command;
      	    CHIETKHAULOAIMATHANGList list =  SelectListByCommand(command);
	        return list;
        }

        public CHIETKHAULOAIMATHANGList SelectByCondition(CHIETKHAULOAIMATHANGConditionForm condt)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM CHIETKHAULOAIMATHANG " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            CHIETKHAULOAIMATHANGList list = SelectListByCommand(command);
            return list;
        }

        private void DeleteByCondition(CHIETKHAULOAIMATHANGConditionForm condt, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM CHIETKHAULOAIMATHANG " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(CHIETKHAULOAIMATHANGConditionForm obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            	if (obj.IsSetMACHIETKHAU)
	{
    	    s += " AND MACHIETKHAU = @MACHIETKHAU ";
	}
	if (obj.IsSetMALOAIMATHANG)
	{
    	    s += " AND MALOAIMATHANG = @MALOAIMATHANG ";
	}
	if (obj.IsSetMUCQUYDINHTU)
	{
    	    s += " AND MUCQUYDINHTU = @MUCQUYDINHTU ";
	}
	if (obj.IsSetMUCQUYDINHDEN)
	{
    	    s += " AND MUCQUYDINHDEN = @MUCQUYDINHDEN ";
	}
	if (obj.IsSetCHIETKHAU1)
	{
    	    s += " AND CHIETKHAU1 = @CHIETKHAU1 ";
	}
	if (obj.IsSetCHIETKHAU2)
	{
    	    s += " AND CHIETKHAU2 = @CHIETKHAU2 ";
	}
	if (obj.IsSetCHIETKHAU3)
	{
    	    s += " AND CHIETKHAU3 = @CHIETKHAU3 ";
	}
	if (obj.IsSetISUSE)
	{
    	    s += " AND ISUSE = @ISUSE ";
	}
            if (s.Length > 0)
            {
                s =" WHERE "+s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, CHIETKHAULOAIMATHANGConditionForm obj)
        {
	    if (obj == null) { return;}
            	            if (obj.IsSetMACHIETKHAU)
            {
            	DalTools.addDbDataParameter(command, "MACHIETKHAU", obj.MACHIETKHAU, DbType.Int32); 
            }
            if (obj.IsSetMALOAIMATHANG)
            {
            	DalTools.addDbDataParameter(command, "MALOAIMATHANG", obj.MALOAIMATHANG, DbType.Int32); 
            }
            if (obj.IsSetMUCQUYDINHTU)
            {
            	DalTools.addDbDataParameter(command, "MUCQUYDINHTU", obj.MUCQUYDINHTU, DbType.Decimal); 
            }
            if (obj.IsSetMUCQUYDINHDEN)
            {
            	DalTools.addDbDataParameter(command, "MUCQUYDINHDEN", obj.MUCQUYDINHDEN, DbType.Decimal); 
            }
            if (obj.IsSetCHIETKHAU1)
            {
            	DalTools.addDbDataParameter(command, "CHIETKHAU1", obj.CHIETKHAU1, DbType.Double); 
            }
            if (obj.IsSetCHIETKHAU2)
            {
            	DalTools.addDbDataParameter(command, "CHIETKHAU2", obj.CHIETKHAU2, DbType.Double); 
            }
            if (obj.IsSetCHIETKHAU3)
            {
            	DalTools.addDbDataParameter(command, "CHIETKHAU3", obj.CHIETKHAU3, DbType.Double); 
            }
            if (obj.IsSetISUSE)
            {
            	DalTools.addDbDataParameter(command, "ISUSE", obj.ISUSE, DbType.Boolean); 
            }
        }
        #endregion
    }
}